package com.wcgksoft.mobileservice.Account.patrol;

import com.wcgksoft.common.util.Layui;
import com.wcgksoft.common.util.comm.StringUtils;
import com.wcgksoft.entity.GreedLand.GreedLand;
import com.wcgksoft.entity.Patrol.CompletePatrolImage;
import com.wcgksoft.entity.Patrol.PatrolAccount;
import com.wcgksoft.entity.Patrol.PatrolImage;
import com.wcgksoft.entity.system.Enterprise;
import com.wcgksoft.entity.system.EnterpriseProject;
import com.wcgksoft.entity.system.SysUser;
import com.wcgksoft.service.GreedLand.GreedlandService;
import com.wcgksoft.service.Patrol.CompletePatrolImageService;
import com.wcgksoft.service.Patrol.PatrolAccountService;
import com.wcgksoft.service.Patrol.PatrolImageService;
import com.wcgksoft.service.openAccount.OpenAccountService;
import net.sf.json.JSONObject;
import org.hibernate.criterion.DetachedCriteria;

import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 御风
 * @version V1.0
 * @Package com.wcgksoft.mobileservice.Account.patrol
 * @Description TODO 管理端巡查列表
 * @date 2020/4/6 12:53
 * @ClassName AppAdminPatrolController
 */
@RequestMapping("mobile/admin")
@Controller
public class AppAdminPatrolController {

    @Autowired
    private PatrolImageService patrolImageService;

    @Autowired
    private PatrolAccountService patrolAccountService;

    @Autowired
    private CompletePatrolImageService completePatrolImageService;

    @Autowired
    private GreedlandService greedlandService;

    @Autowired
    private OpenAccountService openAccountService;

    @ResponseBody
    @RequestMapping("findAll")
    public Layui findAllPatrol(@RequestParam(value = "uesid",required = false)Integer uesid,
                               @RequestParam(value = "areaId",required = false)Integer areaId,
                               @RequestParam(value = "proId",required = false)Integer project_id
    ) {
        DetachedCriteria criteria = DetachedCriteria.forClass(PatrolAccount.class);
        criteria.add(Restrictions.eq("enterpriseId", uesid));
        criteria.add(Restrictions.eq("areaId", areaId));
        criteria.add(Restrictions.eq("proId", project_id));
        List<PatrolAccount> list = patrolAccountService.findByConditions(criteria);
        for (PatrolAccount patrolAccount : list) {
            DetachedCriteria criteria1 = DetachedCriteria.forClass(PatrolImage.class);
            criteria1.add(Restrictions.eq("urisid",patrolAccount.getUrisId()));
            criteria1.add(Restrictions.eq("pactrolId",patrolAccount.getId()));
            List<PatrolImage> byConditions = patrolImageService.findByConditions(criteria1);
            patrolAccount.setPatrolImages(byConditions);
        }
        for (PatrolAccount patrolAccount : list) {
            DetachedCriteria criteria2 = DetachedCriteria.forClass(CompletePatrolImage.class);
            criteria2.add(Restrictions.eq("urisid",patrolAccount.getUrisId()));
            criteria2.add(Restrictions.eq("pactrolId",patrolAccount.getId()));
            List<CompletePatrolImage> byConditions = completePatrolImageService.findByConditions(criteria2);
            patrolAccount.setCompletePatrolImageList(byConditions);
        }
        return Layui.data(0, "查询成功", patrolAccountService.getTotalCount(), list);
    }


    /**
     * 查询绿地名称
     * @param proId
     * @param areaid
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "list")
    public Layui list(@RequestParam(value = "proId",required = false)String proId,
                      @RequestParam(value = "areaid",required = false)String areaid) {
        DetachedCriteria criteria = DetachedCriteria.forClass(GreedLand.class);
        criteria.add(Restrictions.eq("departid", proId));
        criteria.add(Restrictions.eq("areaid", areaid));
        criteria.add(Restrictions.eq("state",0));
        List<GreedLand> list = greedlandService.findByConditions(criteria);
        return Layui.data(0, "查询成功", list);
    }

    /**
     * 选择问题接收方
     * @param greenid
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "select")
    public Layui sec(@RequestParam(value = "greenId",required = false)Integer greenid,
                     @RequestParam(value = "enterprise",required = false)Integer enterprise,
                     @RequestParam(value = "enterpriseProjects",required = false)Integer enterpriseProjects) {
        DetachedCriteria criteria = DetachedCriteria.forClass(SysUser.class);
        criteria.createAlias("sysRole.sysRoleCode","code");
        criteria.add(Restrictions.eq("enterprise.id", enterprise));
        criteria.add(Restrictions.eq("areaid", enterpriseProjects));
        criteria.add(Restrictions.eq("code.role_number","construction"));
        List<SysUser> userList = new ArrayList<>();
        List<SysUser> list = openAccountService.findByConditions(criteria);
        for (SysUser user : list) {
            String[] split = user.getGreenlandid().split(",");
            for (int i = 0; i < split.length; i++) {
                if (Integer.parseInt(split[i])==greenid){
                    userList.add(user);
                }
            }
        }
        return Layui.data(0, "查询成功", userList);
    }


    @ResponseBody
    @RequestMapping(value = "savePatrol")
    public Layui savePatrol(@RequestParam(value = "data",required = false)String data) {
        JSONObject param = JSONObject.fromObject(data);
        if (param==null){
            return Layui.data(1, "请求格式有误", 0, null);
        }else{
            System.out.println(param);

            return Layui.data(0, "查询成功", null);
        }
    }


    @ResponseBody
    @RequestMapping(value = "uploadPatrolImage")
    public Layui uploadPatrolImage(){
        return Layui.data(0, "上传成功", null);
    }
}
